package team.scau.laboratory_repair.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import team.scau.laboratory_repair.common.base.BaseService;
import team.scau.laboratory_repair.common.constant.ConstantRole;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.common.utils.ztree.ZTreeNode;
import team.scau.laboratory_repair.modules.po.Perm;
import team.scau.laboratory_repair.modules.po.QRole;
import team.scau.laboratory_repair.modules.po.Role;
import team.scau.laboratory_repair.modules.po.User;
import team.scau.laboratory_repair.modules.repository.RoleRepository;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author 30254
 * creadtedate:2018/8/19
 */
@Service
public class RoleService extends BaseService<Role, RoleRepository> {

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private PermService permService;

    private QRole qRole = QRole.role;

    @Override
    public RoleRepository getRepository() {
        return roleRepository;
    }

    @Override
    public EntityPathBase<Role> getQBase() {
        return qRole;
    }

    public QueryResults<Role> list(MyPage page) throws Exception {
        JPAQuery<Role> jpaQuery = queryFactory.select(qRole).from(qRole)
                .where(qRole.valid.eq(true))
                .where(qRole.id.ne(1L));
        if (page != null) {
            jpaQuery = jpaQuery.orderBy(page.getSort()).offset(page.getPage()).limit(page.getSize());
        }
        return jpaQuery.fetchResults();

    }

    @Override
    public List<Role> findAll() {
        JPAQuery<Role> jpaQuery = queryFactory.select(qRole).from(qRole)
                .where(qRole.valid.eq(true))
                .where(qRole.id.ne(1L));
        return jpaQuery.fetch();
    }

    /**
     * 获取权限树，并设置已选权限
     *
     * @param checkIds
     * @return
     * @throws Exception
     */
    public List<ZTreeNode> selectPermTree(String checkIds, User cUser) throws Exception {

        List<Perm> perms = permService.list();

        if (cUser != null) {
            StringBuilder ids = new StringBuilder();
            if (cUser.getRole() != null) {
                if (cUser.getRole().equals(ConstantRole.ROLE_SUPER)) {
                    for (Perm perm : perms) {
                        ids.append(perm.getId()).append(",");
                    }
                } else {
                    Role role = this.findById(cUser.getRole());
                    ids = new StringBuilder(role.getPerm());
                }
            }
            List<String> idArray;
            List<String> checkIdArray;
            if (ids == null) {
                idArray = new ArrayList<>();
            } else {
                idArray = Arrays.asList(ids.toString().split(","));
            }
            if (checkIds == null) {
                checkIdArray = new ArrayList<>();
            } else {
                checkIdArray = Arrays.asList(checkIds.split(","));
            }
            List<ZTreeNode> tree = new ArrayList<>();
            for (Perm perm : perms) {
                ZTreeNode treeNode = new ZTreeNode();
                treeNode.setId(perm.getId() + "");
                treeNode.setName(perm.getName());
                treeNode.setPid(perm.getPid() + "");
                if (!checkIdArray.isEmpty() && checkIdArray.contains(perm.getId() + "")) {
                    treeNode.setChecked(true);
                }
                if ((!idArray.isEmpty() && idArray.contains(perm.getId() + ""))) {
                    tree.add(treeNode);
                }
            }
            return tree;
        } else {
            return null;
        }
    }

}
